//
//  WZPSubTagViewController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/7.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPSubTagViewController.h"
#import "WZPSubTagCell.h"
#import <AFNetworking.h>
#import "WZPSubTagItem.h"
#import <SVProgressHUD.h>
#import <MJExtension/MJExtension.h>

static NSString * ID = @"cell";

@interface WZPSubTagViewController ()

@property (nonatomic, weak) AFHTTPSessionManager *mgr;
@property (nonatomic ,strong) NSArray *tags;

@end

@implementation WZPSubTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐订阅";
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"WZPSubTagCell" bundle:nil] forCellReuseIdentifier:ID];
    [self loaddta];
    // 重写setFrame(定义多宽的导航条就加多少宽度)
    // 1) 取消系统分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 2) 设置背景颜色(分割线颜色)
    self.tableView.backgroundColor = [UIColor redColor];
    // 提示用户
    [SVProgressHUD showWithStatus:@"请稍后..."];
    

}
// 当退出时 取消 提示
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    //同时请求取消
    [_mgr.tasks makeObjectsPerformSelector:@selector(cancel)];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//添加数据
-(void)loaddta{
    // 设置请求会话对象
    AFHTTPSessionManager * mar = [AFHTTPSessionManager manager];
    _mgr = mar;
    // 拼接请求参数
    NSMutableDictionary * mudict = [NSMutableDictionary dictionary];
    mudict[@"a"] = @"tag_recommend";
    mudict[@"action"] = @"sub";
    mudict[@"c"] = @"topic";
    
    [mar GET:WZPHtp parameters:mudict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _tags = [WZPSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
    // 刷新表格
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];

    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tags.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 设置标识
    
    WZPSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 获取当前版本号
//    // 布置全屏分割线
//    NSInteger versions = [[UIDevice currentDevice].systemVersion integerValue];
//    // ios7 添加的空白
//    self.tableView.separatorInset = UIEdgeInsetsZero;
//    // ios 8 添加的空白
//    if (versions >= 8.0) {
//        cell.layoutMargins  = UIEdgeInsetsZero;
//    }
    

    cell.item = _tags[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60 + 10;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
